<?php

namespace Biz\service\wiki;


use Biz\model\wiki\ContentWikiMiddle as dbContentWikiMiddle;
use Doraemon\tools\IdParse;


/**
 * Class ContentWikiMiddle
 * @package Biz\service\wiki
 */
class ContentWikiMiddle
{

    //根据obj_id查找关联商品id
    public static function getRelateGoodsByObjId($obj_id)
    {
        $goods_ids = [];
        $relation_infos = dbContentWikiMiddle::getByObjId($obj_id);
        if($relation_infos && is_array($relation_infos)) {
            $goods_ids = array_values(array_unique(array_column($relation_infos, 'goods_id')));
        }
        return $goods_ids;
    }

    //发布时添加商品
    public static function insertGoods($obj_id, $goods_ids, $uid)
    {
        $goods_ids = array_filter(array_unique($goods_ids));
        if($goods_ids && is_array($goods_ids)) {
            $obj_type = IdParse::getType($obj_id);
            $addtime = date('Y-m-d H:i:s', time());
            $relate_status = dbContentWikiMiddle::STATUS_NORMAL;
            foreach($goods_ids as $good_id) {
                dbContentWikiMiddle::insertData([
                    "obj_id" => $obj_id,
                    "obj_type" => $obj_type,
                    "goods_id" => $good_id,
                    "addtime" => $addtime,
                    "uid" => $uid,
                    "relate_status" => $relate_status
                ]);
            }
        }
        return true;

    }


    public static function removeGoods($obj_id)
    {
        $old_goods = dbContentWikiMiddle::getByObjId($obj_id);
        if($old_goods && is_array($old_goods)) {

            dbContentWikiMiddle::updateData([
                "relate_status" => dbContentWikiMiddle::STATUS_DELETE,
                "edittime" => date('Y-m-d H:i:s', time())
            ],
                $obj_id
            );

        }
    }

    //根据obj_ids查找关联商品id
    public static function getRelateGoodsByObjIds($obj_ids = [])
    {
        $goods_ids = [];
        $relation_infos = dbContentWikiMiddle::getByObjIds($obj_ids);

        if($relation_infos && is_array($relation_infos)) {
            foreach($relation_infos as $relation_info) {
                $goods_ids[$relation_info['obj_id']][] = $relation_info['goods_id'];
            }
        }
        return $goods_ids;
    }




}
